﻿using System;
using System.Collections.Generic;
using System.Text;
using OL.FrameCore.Application.Module.Dto;
using OL.FrameCore.Domain.Entity;
//using OL.FrameCore.Domain.IRepository;
using OL.FrameCore.Infrastructure.UnitOfWork;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace OL.FrameCore.Application.Module
{
    public class ModuleService : IModuleService
    {
        IUnitOfWork _unitOfwork;
        public ModuleService(IUnitOfWork unitOfwork)
        {
            _unitOfwork = unitOfwork;
        }

        /// <summary>
        /// 获取所有模块列表
        /// </summary>
        /// <returns></returns>
        public IList<GetModuleListModel> GetAllModuleList()
        {
            var repositoryModule = _unitOfwork.GetRepository<SysModule>();
            var query = from a in repositoryModule.TableNoTracking
                        where a.IsDelete == false
                        orderby a.SortIndex
                        select new GetModuleListModel
                        {
                            Id = a.Id,
                            ModuleName = a.ModuleName,
                            ModuleCode = a.ModuleCode,
                            ParentId = a.ParentId,
                            IsMenu = a.IsMenu,
                            MenuPath = a.MenuPath,
                            Icon = a.Icon,
                            SortIndex = a.SortIndex
                        };
            return query.ToList();
        }
        /// <summary>
        /// 获取模块列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public PageData<GetModuleListModel> GetModuleList(GetModuleListRequest request)
        {
            var repositoryModule = _unitOfwork.GetRepository<SysModule>();
            var query = from a in repositoryModule.TableNoTracking
                        join b in repositoryModule.TableNoTracking on a.ParentId equals b.Id into tmpB
                        from b in tmpB.DefaultIfEmpty()
                        where a.IsDelete == false
                        //&& (request.Id.HasValue ? a.Id == request.Id : true)
                        //&& (string.IsNullOrEmpty(request.ModuleName) ? true : a.ModuleName == request.ModuleName)
                        //&& (string.IsNullOrEmpty(request.ModuleCode) ? true : a.ModuleCode == request.ModuleCode)
                        orderby a.UpdateTime descending
                        select new GetModuleListModel
                        {
                            Id = a.Id,
                            ModuleName = a.ModuleName,
                            ModuleCode = a.ModuleCode,
                            ParentId = a.ParentId,
                            ParentName = b.ModuleName ?? "根模块",
                            IsMenu = a.IsMenu,
                            MenuPath = a.MenuPath,
                            Icon = a.Icon,
                            SortIndex = a.SortIndex,
                            CreateTime = a.CreateTime
                        };

            if (request.Id.HasValue)
                query = query.Where(p => p.Id == request.Id);
            if(!string.IsNullOrEmpty(request.ModuleName))
                query = query.Where(p => p.ModuleName == request.ModuleName);
            if (!string.IsNullOrEmpty(request.ModuleCode))
                query = query.Where(p => p.ModuleCode == request.ModuleCode);

            return repositoryModule.PagingAsync<GetModuleListModel>(query, request.PageIndex, request.PageSize).Result;
        }
        /// <summary>
        /// 获取模块
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public GetModuleResponse GetModule(int id)
        {
            var repositoryModule = _unitOfwork.GetRepository<SysModule>();
            var model = repositoryModule.TableNoTracking.Where(p => p.Id == id && p.IsDelete == false).SingleOrDefault();
            if (model == null)
                throw new Exception($"未找到模块 id:{id}");

            return new GetModuleResponse
            {
                Id = model.Id,
                ModuleName = model.ModuleName,
                ModuleCode = model.ModuleCode,
                Icon = model.Icon,
                IsMenu = model.IsMenu,
                MenuPath = model.MenuPath,
                ParentId = model.ParentId,
                SortIndex = model.SortIndex,
                Remark = model.Remark
            };
        }
        /// <summary>
        /// 添加模块
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public Result AddModule(AddModuleRequest request)
        {
            var repositoryModule = _unitOfwork.GetRepository<SysModule>();

            if (repositoryModule.TableNoTracking.Any(p => p.ModuleCode == request.ModuleCode && p.IsDelete == false))
            {
                return new FailResult("模块编号已存在");
            }

            repositoryModule.Insert(new SysModule
            {
                ModuleName = request.ModuleName,
                ModuleCode = request.ModuleCode,
                ParentId = request.ParentId,
                IsMenu = request.IsMenu,
                MenuPath = request.MenuPath ?? "",
                Icon = request.Icon ?? "",
                SortIndex = request.SortIndex,
                Remark = request.Remark ?? "",
                CreateId = request.OpeatorId,
                CreateTime = DateTime.Now,
                UpdateId = request.OpeatorId,
                UpdateTime = DateTime.Now,
                IsDelete = false
            });

            return new SuccessResult();
        }
        /// <summary>
        /// 修改模块
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public Result UpdateModule(UpdateModuleRequest request)
        {
            var repositoryModule = _unitOfwork.GetRepository<SysModule>();
            var model = repositoryModule.TableNoTracking.Where(p => p.Id == request.Id && p.IsDelete == false).SingleOrDefault();
            if (model == null)
            {
                return new FailResult("找不到数据，请刷新后重试");
            }

            model.ModuleName = request.ModuleName;
            model.ModuleCode = request.ModuleCode;
            model.IsMenu = request.IsMenu;
            model.MenuPath = request.MenuPath ?? "";
            model.Icon = request.Icon ?? "";
            model.SortIndex = request.SortIndex;
            model.ParentId = request.ParentId;
            model.Remark = request.Remark ?? "";
            model.UpdateId = request.OpeatorId;
            model.UpdateTime = DateTime.Now;
            repositoryModule.Update(model);

            return new SuccessResult();
        }
        /// <summary>
        /// 删除模块
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Result DeleteModule(int id, int opeatorId)
        {
            var repositoryModule = _unitOfwork.GetRepository<SysModule>();
            var model = repositoryModule.Table.Where(p => p.Id == id && p.IsDelete == false).SingleOrDefault();
            if (model == null)
            {
                return new FailResult("找不到数据，请刷新后重试");
            }

            model.IsDelete = true;
            model.UpdateId = opeatorId;
            model.UpdateTime = DateTime.Now;
            _unitOfwork.Save();

            return new SuccessResult();
        }
    }
}
